import { createRouter, createWebHashHistory } from "vue-router";

const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      name: "登录",
      component: () => import("@/views/Login.vue"),
      // meta: { requiresAuth: false },
      // children:[
      //    {
      //     path: "/admin/login",
      //     name: "",
      //     component: () => import("@/views/employee/components/.vue"),
      //   },
      // ],
    },

    {
      path: "/emp",
      name: "员工",
      component: () => import("@/views/employee/Employee.vue"),
      // meta: { requiresAuth: true },
      children: [
        {
          path: "home",
          name: "Home",
          component: () => import("@/views/employee/components/Home.vue"),
        },
        {
          path: "leave",
          name: "LeaveApply",
          component: () =>
            import("@/views/employee/components/insertLeaveApply.vue"),
        },

        {
          path: "resign",
          name: "ResignApply",
          component: () =>
            import("@/views/employee/components/ResignApply.vue"),
        },
        {
          path: "attend",
          name: "Attendance",
          component: () => import("@/views/employee/components/Attendance.vue"),
        },
        {
          path: "/personal",
          name: "Personal",
          component: () => import("@/views/employee/components/Personal.vue"),
        },
      ],
    },

    {
      path: "/userManager",
      name: "管理员管理",
      component: () => import("@/views/admin/Admin.vue"),
      children: [
        {
          path: "home",
          name: "AdminHome",
          component: () => import("@/views/admin/components/AdminHome.vue"),
        },
        
        {
          path: "employee",
          name: "employee",
          component: () =>
            import("@/views/admin/components/EmployeeManage.vue"),
        },
        {
          path: "leave",
          name: "LeaveManager",
          component: () => import("@/views/admin/components/LeaveManage.vue"),
        },
        {
          path: "attendance",
          name: "AttendanceManager",
          component: () =>
            import("@/views/admin/components/AttendanceManage.vue"),
        },
        {
          path: "offboarding",
          name: "offboarding",
          component: () => import("@/views/admin/components/Offboarding.vue"),
        },
        {
          path: "onboarding",
          name: "onboarding",
          component: () => import("@/views/admin/components/Onboarding.vue"),
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  const isLogin = !!localStorage.getItem("token");
  if (to.meta.requiresAuth && !isLogin) {
    // 需要登录但未登录，跳回登录页
    next("/admin/login");
  } else {
    // 已登录或无需登录，正常跳转
    next();
  }
});

export default router;
